package drds.global_transaction.lock;

import drds.dbutils.map_list_sql.MapLists;
import drds.global_transaction.table.global_transaction_lock;
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.feisoft.common.utils.DbPool.Db;
import org.feisoft.common.utils.SpringBeanUtil;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public abstract class Lock implements ILock
{

    //唯一标识和创建时间
    @Setter
    @Getter
    protected Long id;
    @Setter
    @Getter
    protected long createDateTimeLong;
    //独占锁标志
    @Setter
    @Getter
    protected String tableName;
    @Setter
    @Getter
    protected String primaryKeyColumnValue;
    @Setter
    @Getter
    protected String exclusiveLock;
    //
    @Setter
    @Getter
    protected Integer shareLock;
    //
    @Setter
    @Getter
    protected String gloableTransactionId;
    @Setter
    @Getter
    protected String branchTransactionId;
    private Db db = null;
    //
    @Setter
    @Getter
    private boolean isLock;

    {
        if (this.db == null)
        {
            Db db = (Db) SpringBeanUtil.getBean("db");
            this.db = db;
        }
    }
    //

    @Override
    public abstract void lock() throws SQLException;

    @Override
    public abstract void unlock() throws SQLException;

    protected void insertLock() throws SQLException
    {

        //        synchronized (Lock.class) {
        String sql = "select count(0) as total from  global_transaction_lock where table_name = '" + tableName + "' and primary_Key_Column_Value ="
                + primaryKeyColumnValue + " and exclusive_Lock='" + exclusiveLock + "'";
        int total = db.countList(sql);
        if (total > 0)
        {
            throw new SQLException();
        }

        String insertSql = "insert into global_transaction_lock (table_name,primary_Key_Column_Value,gloable_Transaction_Id,branch_Transaction_Id,exclusive_Lock,share_Lock,create_date_time_long)values(";
        insertSql +=
                "'" + tableName + "'," + primaryKeyColumnValue + ",'" + gloableTransactionId + "','" + branchTransactionId + "','" + exclusiveLock + "'," + shareLock + ","
                        + createDateTimeLong;
        insertSql += ")";
        try
        {
            int id = db.executeUpdate(insertSql);
            if (id > 0)
            {
                setId((long) id);
            }
        } catch (Exception e)
        {
            log.error("插入lock失败，totol_sql ={},insert_sql={},total={}", sql, insertSql, total, e);
            throw new SQLException();

        }
        //        }
    }

    ;

    protected void deleteLock() throws SQLException
    {
        //String sql = "delete from global_transaction_lock  where id = " + id;
        //db.executeUpdate(sql);

        String sql = global_transaction_lock.get$deleteLock$sql();
        Connection connection = null;
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(global_transaction_lock.id, id);
        int result = MapLists.set(connection, sql, map);

    }

}
